package com.nimitz.web.controller.equipment;

import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.factorEquipment.domain.*;
import com.nimitz.factorEquipment.service.EquipmentService;
import com.nimitz.factorEquipment.service.serviceImpl.OrderNumberGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/smart/equipment")
public class EquipmentController extends BaseController {
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private OrderNumberGeneratorService orderNumberGeneratorService;

    // 获取所有设备
    @GetMapping("/list")
    public TableDataInfo getAllEquipments(Equipment equipment) {
        startPage();
        List<Equipment> equipments = equipmentService.getAllEquipments(equipment);
        return getDataTable(equipments);
    }

    // 根据id获取设备
    @GetMapping("/get")
    public AjaxResult getEquipmentById(Long id){
        Equipment equipment = equipmentService.selectEquipmentById(id);
        return AjaxResult.success(equipment);
    }
    // 添加设备
    @PostMapping("/add")
    public AjaxResult addNotice(@RequestBody Equipment equipment) {
        final String string = orderNumberGeneratorService.generateOrderNumber();
        equipment.setNumbering(string);
        equipmentService.addEquipment(equipment);
        return AjaxResult.success("操作成功");
    }
    //修改设备信息
    @PutMapping("/update")
    public AjaxResult updateEquipment(@RequestBody Equipment equipment){
        equipmentService.updateEquipment(equipment);
        return AjaxResult.success("修改成功");
    }

    // 删除设备
    @DeleteMapping("/delete")
    public AjaxResult deleteEquipment(@RequestParam Long id) {
        equipmentService.deleteEquipment(id);
        return AjaxResult.success("删除成功");
    }
//    @GetMapping("/detail")
//    public TableDataInfo getAllEquipmentDetails(@RequestParam String id) {
//        startPage();
//        final List<EquipmentDetail> allEquipmentDetails = equipmentService.getAllEquipmentDetails(id);
//        return getDataTable(allEquipmentDetails);
//    }

    // 新增端点：根据id查询设备名称和关联的factor_id和value
    @GetMapping("/detailsById")
    public AjaxResult getEquipmentDetailsById(@RequestParam Long id) {
        List<EquipmentDetail> equipmentDetails = equipmentService.getEquipmentDetailsId(id);
        return AjaxResult.success(equipmentDetails);
    }

    //    查询所有设备类型名称
    @GetMapping("/selectType")
    public AjaxResult selectType(){
        List<EquipmentType> equipmentTypes = equipmentService.selectAllEquipmentTypes();
        return AjaxResult.success(equipmentTypes);
    }

    // 查询所有断面名称
    @GetMapping("/selectSection")
    public AjaxResult selectSection(){
        List<Section> sections = equipmentService.selectAllSections();
        return AjaxResult.success(sections);
    }




    @GetMapping("/provinces")
    public List<Area> getProvinces() {
        return equipmentService.getProvinces();
    }

    @GetMapping("/cities")
    public List<Area> getCities( Long id) {
        return equipmentService.getCitiesByProvinceId(id);
    }

    @GetMapping("/districts")
    public List<Area> getDistricts( Long id) {
        return equipmentService.getDistrictsByCityId(id);
    }

    @GetMapping("/rivers")
    public AjaxResult getRivers(@RequestParam(value = "id") Long areaId) {
        return AjaxResult.success(equipmentService.getRiversByDistrictId(areaId));
    }

    @GetMapping("/sections")
    public AjaxResult getSections(@RequestParam(value = "id") Long riverId) {
        List<Section> sections = equipmentService.getSectionsByRiverId(riverId);
        return AjaxResult.success(sections);
    }
}
